#include "io.h"

//
// Input buttons for players
//

#ifdef USE_API_IO_SDL

#ifdef UGLY_HACK_FOR_THOSE_WHO_DONT_KNOW_HOWTO_INSTALL_SDL
#include <SDL.h>
#else
#include <SDL/SDL.h>
#endif

static int _io_player_keys[PLAYER_MAX] = {0, 0, 0, 0};
static SDL_Joystick *_io_joysticks[32];

void _io_init(void)
{
    for(int i = 0; i < SDL_NumJoysticks(); i++)
    {
        _io_joysticks[i] = SDL_JoystickOpen(i);
        printf("%d %s\n", i, SDL_JoystickName(i));
    }
}

void _io_exit(void)
{
    for(int i = 0; i < SDL_NumJoysticks(); i++)
    {
        SDL_JoystickClose(_io_joysticks[i]);
    }
}

int io_player_get_buttons(int player)
{
    return _io_player_keys[player];
}

void io_player_read_buttons(void)
{
    int keys_size;
    Uint8 *keys = SDL_GetKeyState(&keys_size);
    _io_player_keys[0] = _io_player_keys[1] = _io_player_keys[2] = _io_player_keys[3] = 0;
    SDL_PumpEvents();

    // Joueur 1
    if(keys[SDLK_LEFT])                 _io_player_keys[0] |= IO_PLAYER_LEFT;
    if(keys[SDLK_RIGHT])                _io_player_keys[0] |= IO_PLAYER_RIGHT;
    if(keys[SDLK_UP])                   _io_player_keys[0] |= IO_PLAYER_TOP;
    if(keys[SDLK_DOWN])                 _io_player_keys[0] |= IO_PLAYER_BOTTOM;
    if(keys[SDLK_BACKSPACE])            _io_player_keys[0] |= IO_PLAYER_CIRCLE;
    if(keys[SDLK_RETURN])               _io_player_keys[0] |= IO_PLAYER_CROSS;
    if(keys[SDLK_ESCAPE])               _io_player_keys[0] |= IO_PLAYER_PAUSE;

    // Joueur 2
    if(keys[SDLK_q])                    _io_player_keys[1] |= IO_PLAYER_LEFT;
    if(keys[SDLK_e])                    _io_player_keys[1] |= IO_PLAYER_RIGHT;

    // Joueur 3
    if(keys[SDLK_i])                    _io_player_keys[2] |= IO_PLAYER_LEFT;
    if(keys[SDLK_p])                    _io_player_keys[2] |= IO_PLAYER_RIGHT;

    // Joueur 4
    if(keys[SDLK_g])                    _io_player_keys[3] |= IO_PLAYER_LEFT;
    if(keys[SDLK_j])                    _io_player_keys[3] |= IO_PLAYER_RIGHT;

    // Read joystick
    for(int i = 0; i < SDL_NumJoysticks() && i < 4; i++)
    {
        Uint8 hat = SDL_JoystickGetHat(_io_joysticks[i], 0);
        if(hat & SDL_HAT_UP)    _io_player_keys[i] |= IO_PLAYER_TOP;
        if(hat & SDL_HAT_DOWN)  _io_player_keys[i] |= IO_PLAYER_BOTTOM;
        if(hat & SDL_HAT_LEFT)  _io_player_keys[i] |= IO_PLAYER_LEFT;
        if(hat & SDL_HAT_RIGHT) _io_player_keys[i] |= IO_PLAYER_RIGHT;

        if(SDL_JoystickGetButton(_io_joysticks[i], 14))  _io_player_keys[i] |= IO_PLAYER_CROSS;
        if(SDL_JoystickGetButton(_io_joysticks[i], 13))  _io_player_keys[i] |= IO_PLAYER_CIRCLE;
    }

    return;
}

void io_player_get_joystick(int player, vec2 joystick)
{
    joystick[0] = SDL_JoystickGetAxis(_io_joysticks[player], 0) / 32768.;
    joystick[1] = SDL_JoystickGetAxis(_io_joysticks[player], 1) / 32768.;
}

void io_get_mouse(float *x, float *y, int *buttons_state)
{
    int mx, my, mbuttons;
    GFX_Resolution res;

    gfx_get_resolution(&res);
    mbuttons = SDL_GetMouseState(&mx, &my);

    *x = mx / (res.width / 640.);
    *y = my / (res.height / 360.);

    *buttons_state = 0;

    if(SDL_BUTTON(SDL_BUTTON_LEFT) & mbuttons)
    {
        *buttons_state |= IO_MOUSE_LBUTTON;
    }
    if(SDL_BUTTON(SDL_BUTTON_RIGHT) & mbuttons)
    {
        *buttons_state |= IO_MOUSE_RBUTTON;
    }

}

#endif
